Passed
Push — add-failing-tests ( 3ed854 )
by Eric
01:20
created

describe(ꞌutilities.toObjectꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 59
rs 9.597

3 Functions

Rating   Name   Duplication   Size   Complexity  
B ��) 0 28 1
A ��) 0 14 1
A ��) 0 13 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var utilities = require('../utilities.js');
2
3
describe('utilities.toObject', function() {
4
    describe('failing tests', function() {
5
        it("should fail because the arr parameter is undefined", function() {
6
            expect(utilities.toObject).toThrowError(TypeError);
7
        });
8
9
        it("should fail because the arr parameter is not an Array", function() {
10
            expect(utilities.toObject.bind(null, {})).toThrowError(TypeError);
11
        });
12
13
        it("should fail because the mapBy not of type String and not falsy", function() {
14
            expect(utilities.toObject.bind(null, [], {})).toThrowError(TypeError);
15
        });
16
    });
17
18
    describe('array of primitives toObject', function() {
19
        // test example from README.md
20
        var states = ['Sachsen', 'Sachsen-Anhalt', 'Berlin', 'Hamburg'];
21
        var statesObject = utilities.toObject(states);
22
        // end test example from README.md
23
24
        it("should be able to access object by index", function() {
25
            expect(statesObject[0]).toEqual('Sachsen');
26
        });
27
28
        it("should have 4 keys", function() {
29
            expect(Object.keys(statesObject).length).toEqual(4);
30
        });
31
    });
32
33
    describe('array of objects toObject', function() {
34
        // test example from README.md
35
        var news = [
36
            {
37
                id: 12001,
38
                headline: 'Tiger goes limp',
39
                subHeadline: 'Pulls out after 9 holes'
40
            },{
41
                id: 666,
42
                headline: 'Croc has beef with cow',
43
                subHeadline: ''
44
            },{
45
                id: 1337,
46
                headline: 'Germans wurst at penalties',
47
                subHeadline: 'New stats prove England are better from the spot'
48
            }
49
        ];
50
        var newsObject = utilities.toObject(news, 'id');
51
        // end test example from README.md
52
53
        it("should be able to access object by ID", function() {
54
            expect(newsObject[1337].headline).toEqual('Germans wurst at penalties');
55
        });
56
57
        it("should have 3 keys", function() {
58
            expect(Object.keys(newsObject).length).toEqual(3);
59
        });
60
    });
61
});
62